package com.gitee.xtyihj.servlet;

import com.gitee.xtyihj.bean.User;
import com.gitee.xtyihj.dao.SQLManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserServlet extends HttpServlet
{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        SQLManager.get().connectDB();
        String method = request.getParameter("method");
        if ("login".equals(method))
        {//登录
            String userName = request.getParameter("username");
            String passWord = request.getParameter("password");
            if (userName == null || "".equals(userName.trim()) || passWord == null || "".equals(passWord.trim()))
            {
                System.out.println("用户名或密码不能为空！");
                return;
            }
            else if (SQLManager.get().hasUser(userName,passWord))
            {
                System.out.println("登录成功！");
                request.getSession().setAttribute("username", userName);
                return;
            } else
            {
                System.out.println("用户名或密码错误，登录失败！");
                return;
            }
        }
        SQLManager.get().shutdown();
    }
}
